Ext.define('DragonApp.view.resource.Controller', {
    extend: 'DragonApp.view.foundation.ViewController',

    alias: 'controller.resource',

    //========
    //图像预览
    //========
    onPreview: function(button, e, options) {

        //当前记录
        var record = button.getWidgetRecord();
        var path = record.get('path');
        var me = this,
            view = me.getView();

        me.dialog = view.add({
            xtype: 'window',
            title: record.get('filename') + ' 预览',
            width: record.get('width') + 2,
            height: record.get('height') + 37,
            layout: 'fit',
            html: '<img src="' + DragonApp.ux.config.Config.getTagUrl(record.get('filename')) + '" alt="' + record.get('filename') + '" />'
        });
        me.dialog.show();
    },

    //========
    //回车搜索
    //========
    onTextFieldSpecialKeySearch: function(field, e, options) {
        if (e.getKey() === e.ENTER) {
            this.onSearch();
        }
    },

    //==========
    //工具栏搜索
    //==========
    onSearch: function(button, e, options) {

        //搜索字段
        var searchFilter = Ext.ComponentQuery.query('resource-bar')[0];
        var searchField = searchFilter.queryById('search_field');

        //复位
        var store = Ext.data.StoreManager.lookup('jsonPFileStore');
        store.clearFilter();

        //内容过滤
        store.filterBy(function(item) {

            return (item.get('filename').indexOf(searchField.getValue()) >= 0);
        });
    },

    //==========
    //单记录删除
    //==========
    onDelete: function(button, e, options) {

        //当前记录
        var record = button.getWidgetRecord();
        var param = { url: record.get('path') + '\\' + record.getData().filename };
        var url = DragonApp.ux.config.Config.getUrl('/index/File_Manager/deleteImg/');

        //删除请求
        this.deletePrompt(url, param, record);

    },

    //========
    //批量删除
    //========
    onBatDelete: function(button, e, options) {

        var grid = this.getView();
        var store = grid.getStore();

        var records = grid.getSelectionModel().getSelection();

        //批量获取ID
        var arr = [];

        records.forEach(function(item) {
            arr.push(item.getData().path + '\\' + item.getData().filename);
        });

        var param = {};
        param.path = JSON.stringify(arr);

        var url = DragonApp.ux.config.Config.getUrl('/index/File_Manager/batDeleteImg/');

        this.batDeletePrompt(url, param, records, store);
    },

    onUpload: function(button, e, options){

        var win = Ext.create('Ext.window.Window', {
            title: '图像上传',
            resizable: false,
            collapsible: false,
            animCollapse: false,
            maximizable: false,
            closable: true,
            draggable: true,
            items: [{
                fieldLabel: '上传图像',
                xtype: 'taguploadform',
                destUrl: DragonApp.ux.config.Config.getUrl('/index/Upload/uploadTag/'),
                height: 210,
                width: 420
            }]
        }).show();
    }
});